****************************************************************************
* Purpose: Create sample of births in period 2012-2017 and birth outcomes
****************************************************************************

do "D:\Data\workdata\708296\Project - Mother groups and mental health\Do\global.do" 

use "$raw\MFR2014"
forvalues i =2015/2017 {
append using "$raw\MFR`i'"
}

// Define yob
	rename foedselsaar yob

* year 2012 and after
keep if yob>=2012


drop if DOEDSDATO_BARN==foedselsdato

// Define child id
	rename CPR_BARN pnr
	gen incompletepnr = strlen(pnr)
	drop if incompletepnr<12


// Select variables to keep 
	keep pnr CPR_FADER CPR_MODER BARNSNUMMER_FLERFOLDSFOEDSEL FLERFOLDSFOEDSEL_BEREGNET besoeghosjordemoder besoeghoslaege BMI_MODER BOPAELSKOMMUNE_MODER yob foedselsdato GESTATIONSALDER_DAGE hovedomfang KOEN_BARN MARKOER_GRAVIDITETSKOMPLIKATIO LAENGDE_BARN MARKOER_HJEMMEFOEDSEL_BEREGNET MARKOER_KEJSERSNIT paritet SENGEDAGE_BEREGNET_BARN VAEGT_BARN ALDERVEDDOED_DAGE_BARN

// Define municipality of the  mother
	destring BOPAELSKOMMUNE_MODER, gen(mfr_muni)
	
// Define parity
	destring paritet, gen(mfr_paritet)
	
// Define home births and CS
	replace MARKOER_HJEMMEFOEDSEL_BEREGNET = "0" if MARKOER_HJEMMEFOEDSEL_BEREGNET == ""
	destring MARKOER_HJEMMEFOEDSEL_BEREGNET, gen(mfr_homebirth)

	replace MARKOER_KEJSERSNIT = "0" if MARKOER_KEJSERSNIT ==""
	destring MARKOER_KEJSERSNIT, gen(mfr_cs)

// Define child gender
	replace KOEN_BARN = "0" if KOEN_BARN == "M"
	replace KOEN_BARN = "1" if KOEN_BARN == "K"
	replace KOEN_BARN = "" if KOEN_BARN == "U"
	destring KOEN_BARN, gen(mfr_female)

// Define birthweight and lowbw
	gen bw = VAEGT_BARN
	replace bw = . if bw < 500
	replace bw = . if bw > 6000
	
	gen lowbw = .
	replace lowbw = 1 if bw < 2500
	replace lowbw = 0 if bw > 2499 & bw != .
	rename lowbw mfr_lowbw
	rename bw mfr_bw

// Define first time mothers
	gen firsttime = . 
	replace firsttime = 1 if mfr_paritet == 1
	replace firsttime = 0 if mfr_paritet > 1 & mfr_paritet != .
	rename firsttime mfr_first

// Define gestational age
	gen gestation = GESTATIONSALDER_DAGE
	gen preterm = .
	replace preterm = 1 if gestation < 259
	replace preterm = 0 if gestation > 258 & gestation != .
	rename gestation mfr_gest
	rename preterm mfr_preterm

// Define parental identifiers
	rename CPR_FADER cprf
	rename CPR_MODER cprm

// Define child date of birth
	rename foedselsdato mfr_dob
	gen mfr_weekday = dow(mfr_dob)
	gen mfr_month = month(mfr_dob)
	gen mfr_week = week(mfr_dob)

// Select what to keep
	order pnr yob  cpr* mfr*
	keep pnr yob  cpr* mfr* 
	
	rename yob mfr_yob
	
// Remove duplicats 
	bys pnr: keep if _n==1

// Generate missing indicators
	foreach var of varlist mfr_first mfr_lowbw mfr_preterm mfr_female mfr_homebirth mfr_cs {
		gen `var'_miss = 0
		replace `var'_miss = 1 if `var' == .
		replace `var' = 0 if `var' == . 	
	}

// Rename id variables 
	rename cprm id_mother
	rename cprf id_father
// Save

	compress
	save "$work\mfr_sample.dta", replace //N=355,123
	
